const express = require("express");
const pool = require("../pool.js");

const urlRouter = express.Router();
module.exports = urlRouter;

// 插入图片资源
urlRouter.post("/insert", (req, res, next) => {
  var carname = req.body.carname;
  var kind = req.body.kind;
  var carimg = req.body.carimg;
  if (carname == "" || carimg == "") {
    res.send({ code: 202, msg: "输入不可为空" });
    return;
  }
  var sql2 = `insert into carouselurl values(replace(UUID(),'-',''),?,?,?)`;
  pool.query(sql2, [carname, carimg, kind], (err, r) => {
    if (err) {
      next(err);
      return;
    }
    res.send({ code: 200, msg: "插入数据成功" });
  });
});

// 查找所有图片
urlRouter.get("/findAll", (req, res, next) => {
  var page = req.query.page || 1;
  const pagesize = 10;
  var offset = (page - 1) * pagesize;
  var sql = `select count(carid) as count from carouselurl`;
  pool.query(sql, (err, r) => {
    if (err) {
      next(err);
      return;
    }
    let pagecount = Math.ceil(r[0].count / pagesize);
    var sql = `select * from carouselurl limit ?,?`;
    pool.query(sql, [offset, pagesize], (err, r) => {
      if (err) {
        next(err);
        return;
      }
      res.send({ code: 200, data: r, pagecount });
    });
  });
});
// 根据id查找图片
urlRouter.get("find", (req, res, next) => {
  var carid = req.query.carid;
  if (carid == "") {
    res.send({ code: 202, msg: "id不能为空" });
    return;
  }
  var sql = `select * from carouselurl where carid=?`;
  pool.query(sql, [carid], (err, r) => {
    if (err) {
      next(err);
      return;
    }
    if (r.length != 0) {
      res.send({ code: 200, msg: "查找成功", data: r[0] });
    } else {
      res.send({ code: 201, msg: "无数据" });
    }
  });
});
// 根据图片种类查找图片路径
urlRouter.get("/findKind", (req, res, next) => {
  var kind = req.query.kind;
  var page = req.query.page || 1;
  const pagesize = 8;
  var offset = (page - 1) * pagesize;
  if (kind.trim() == "") {
    res.send({ code: 202, msg: "图片种类不能为空" });
    return;
  }
  var sql = `select carimg from carouselurl where kind=? limit ?,?`;
  pool.query(sql, [kind, offset, pagesize], (err, r) => {
    if (err) {
      next(err);
      return;
    }
    if (r.length != 0) res.send({ code: 200, msg: "查找成功", data: r });
    else res.send({ code: 201, msg: "无数据" });
  });
});
// 修改图片数据
urlRouter.put("/update", (req, res, next) => {
  var carid = req.body.carid;
  var carname = req.body.carname;
  var carimg = req.body.carimg;
  var kind = req.body.kind;
  if (carid == "" || carname == "" || carimg == "") {
    res.send({ code: 202, msg: "输入不能为空" });
    return;
  }
  var sql = `update carouselurl set carname=?, carimg=?, kind=? where carid=?`;
  pool.query(sql, [carname, carimg, kind, carid], (err, r) => {
    if (err) {
      next(err);
      return;
    }
    res.send({ code: 200, msg: "修改成功" });
  });
});
// 删除图片数据
urlRouter.delete("/delete", (req, res, next) => {
  var carid = req.query.carid;
  if (carid == "" || carid == undefined) {
    res.send({ code: 500, msg: "id不可为空" });
    return;
  }
  var sql = `delete from carouselurl where carid=?`;
  pool.query(sql, [carid], (err, r) => {
    if (err) {
      next(err);
      return;
    }
    if (!r.affectedRows) res.send({ code: 201, msg: "无此图片" });
    else res.send({ code: 200, msg: "删除成功" });
  });
});
